import type { AppTypeProps } from '@/types';

import AppModel from '@/model/app';

interface DAMNU_ENABLE {
  [key: string]: any; // 字段扩展声明
}
export default {
  /**
   * @description: 格式化时间
   * @param {any} date
   * @param {string} format
   */
  formatTime(date: any, format: string) {
    if (!date) return;
    if (!format) format = 'yyyy-MM-dd';
    switch (typeof date) {
      case 'string': {
        date = new Date(date.replace(/-/, '/'));
        break;
      }
      case 'number': {
        date = new Date(date);
        break;
      }
      default:
    }
    const dict: DAMNU_ENABLE = {
      d: date.getDate(),
      dd: `${date.getDate() + 100}`.slice(1),
      H: date.getHours(),
      HH: `${date.getHours() + 100}`.slice(1),
      M: date.getMonth() + 1,
      m: date.getMinutes(),
      MM: `${date.getMonth() + 101}`.slice(1),
      mm: `${date.getMinutes() + 100}`.slice(1),
      s: date.getSeconds(),
      ss: `${date.getSeconds() + 100}`.slice(1),
      yyyy: date.getFullYear(),
    };
    return format.replaceAll(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, (...args) => {
      const args1 = args[0];
      return dict[args1];
    });
  },
  /**
   * @description: 从localstorage中获取access_token
   */
  getAccessToken() {
    return localStorage.getItem('AcessToken') || '';
  },
  /**
   * @description: 获取指定key的App
   */
  getAppByKey(key: string) {
    const appList = AppModel.allAppList;
    if (!appList) return false;
    return appList.find((app) => app.key === key);
  },
  /**
   * @description: 获取桌面App列表
   */
  getDeskTopApp() {
    return AppModel.allAppList;
  },
  /**
   * @description: APP是否常驻Dock
   */
  isAppInKeepList(app: AppTypeProps, dockAppList: Array<AppTypeProps>) {
    for (const item of dockAppList) {
      if (item.key === app.key) {
        return true;
      }
    }
    return false;
  },
  /**
   * @description: APP是否打开
   */
  isAppInOpenList(app: AppTypeProps, openAppList: Array<AppTypeProps>) {
    for (const item of openAppList) {
      if (item.key === app.key) {
        return true;
      }
    }
    return false;
  },
  /**
   * @description: 保存access_token到localstorage
   */
  saveAccessToken(access_token: string) {
    localStorage.setItem('AcessToken', access_token);
  },
};
